package com.lanhai.crowd.test;


import com.lanhai.crowd.entity.Admin;
import com.lanhai.crowd.entity.Role;
import com.lanhai.crowd.mapper.AdminMapper;
import com.lanhai.crowd.mapper.RoleMapper;
import com.lanhai.crowd.service.api.AdminService;
import com.lanhai.crowd.util.CrowdUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Random;

//在类上标记必要的注解，spring整合junit
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml", "classpath:spring-persist-tx.xml"})
public class CrowdTest01 {


    @Autowired
    private DataSource dataSource;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleMapper roleMapper;


    Logger log = LoggerFactory.getLogger(CrowdTest01.class);

    @Test
    public void testTx(){

        Admin admin = new Admin(null, "jerry", "jerry123", "杰瑞", "jerry123@qq.com", null);
        adminService.saveAdmin(admin);

    }

    @Test
    public void test03(){

        Admin admin = adminMapper.selectByPrimaryKey(1);

        log.debug(admin.toString());

    }

    @Test
    public void test02(){
        Admin admin = new Admin(null, "zs123", "zs123", "张三", "zs123@qq.cp,", null);
        adminMapper.insert(admin);

    }

    @Test
    public void test01() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }

    /**
     * 插入测试数据，不要运行该方法
     */
    @Test
    public void testAddAdmin() {
        int count = 0;
        for (int i = 1; i < 464; i++) {
            String user = "zgp77"+ (char)(new Random().nextInt(46) + 1) + i;
            String pwd = "cwd" + (new Random().nextInt(999) + 1) + i;
            String encoded = CrowdUtil.md5(pwd);
            Admin admin = new Admin(null, user, encoded, "张三" + i, user + "@qq.com", null);

            count += adminMapper.insert(admin);

        }

        log.info("插入了：" + count + "条");

    }

    /**
     * 测试插入Role数据
     */
    @Test
    public void TestAddRoleData() {
        int count = 0;
        for (int i = 1; i < 123; i++) {

            count += roleMapper.insert(new Role(null, "role" + i));

        }

        log.info("共新增了数据：" + count + "条。");
    }



}
